#!/bin/bash

export LC_ALL=C
export BASE_DATA_PATH=/home/mixagol/data
export BASE_DATE=20120802

##
## BLAST_NW
##
## Старый код, обновить перед запуском!
##
export CUR_DATA_DIR=${BASE_DATA_PATH}/4_normed_matrix/${BASE_DATE}/blast_nw
mkdir -p $CUR_DATA_DIR

zcat ${BASE_DATA_PATH}/1_databases/genes.txt.gz   | cut -f1 | nl -v0 -n ln -w1 > ${CUR_DATA_DIR}/map_int_gene.txt
zcat ${BASE_DATA_PATH}/1_databases/genomes.txt.gz | cut -f1 | nl -v0 -n ln -w1 > ${CUR_DATA_DIR}/map_int_genom.txt

zcat ${BASE_DATA_PATH}/3_raw_matrix_nw/results.txt \
    | cut -f1,2,11 \
    | 3_normed_matrix/apply_maps.py \
            --genes-map ${CUR_DATA_DIR}/map_int_gene.txt \
            --genomes-map ${CUR_DATA_DIR}/map_int_genom.txt \
    | gzip \
    > ${CUR_DATA_DIR}/matrix.txt.gz

for E_VALUE in 1e+01 1e+00 1e-01 5e-02 1e-02 1e-03 1e-04 1e-05 1e-06 1e-07 1e-08 1e-09 1e-10 1e-11 1e-12 1e-13 1e-14 1e-15 1e-20 1e-30 1e-40 1e-50; do
    zcat ${CUR_DATA_DIR}/matrix.txt.gz \
        | awk -F'\t' '{if ($3<='$E_VALUE') print $1"\t"$2}' \
        | 3_normed_matrix/create_matrix.py \
        > ${CUR_DATA_DIR}/matrix_${E_VALUE}_.txt
done


#export GENOMES_NUM=`cat "${BASE_DATA_PATH}/3_normed_matrix/map_int_genom.txt" | wc -l`

#g++ -O3 3_normed_matrix/pre_calc_intersections.cpp -o 3_normed_matrix/pre_calc_intersections
#
#3_normed_matrix/run_distmi_cluster.sh $GENOMES_NUM /home/mixagol/work/data/dist_mutual_information_${GENOMES_NUM}
#ls /home/mixagol/work/data/dist_mutual_information_${GENOMES_NUM}/* \
#    | xargs -n1 cat \
#    | sort -t$'\t' -n -k1,1 -k2,2 \
#    | uniq \
#    > ${BASE_DATA_PATH}/3_normed_matrix/dist_mutual_information.txt
#rm -rf /home/mixagol/work/data/dist_mutual_information_${GENOMES_NUM}/*


##
## NW WU-BLAST norm matrix
##

export CUR_DATA_DIR=${BASE_DATA_PATH}/4_normed_matrix/${BASE_DATE}/wu_blast_nw
mkdir -p $CUR_DATA_DIR

zcat ${BASE_DATA_PATH}/1_databases/${BASE_DATE}/genes.txt.gz   | cut -f1 | nl -v0 -n ln -w1 > ${CUR_DATA_DIR}/map_int_gene.txt
zcat ${BASE_DATA_PATH}/1_databases/${BASE_DATE}/genomes.txt.gz | cut -f1 | nl -v0 -n ln -w1 > ${CUR_DATA_DIR}/map_int_genom.txt

zcat ${BASE_DATA_PATH}/3_raw_matrix_nw/${BASE_DATE}/wu_blast/results.txt.gz \
    | awk -F'\t' '{print $2"\t"$1"\t"$3}' \
    | 4_normed_matrix/apply_maps.py \
            --genes-map ${CUR_DATA_DIR}/map_int_gene.txt \
            --genomes-map ${CUR_DATA_DIR}/map_int_genom.txt \
    | gzip \
    > ${CUR_DATA_DIR}/matrix.txt.gz

for Z_VALUE in 5 10; do
    zcat ${CUR_DATA_DIR}/matrix.txt.gz \
        | awk -F'\t' '{if ($3>='$Z_VALUE') print $1"\t"$2}' \
        | gzip \
        > ${CUR_DATA_DIR}/matrix_raw_${Z_VALUE}_.txt.gz

    zcat ${CUR_DATA_DIR}/matrix_raw_${Z_VALUE}_.txt.gz \
        | 4_normed_matrix/create_matrix.py \
        > ${CUR_DATA_DIR}/matrix_${Z_VALUE}_.txt

    zcat ${CUR_DATA_DIR}/matrix_raw_${Z_VALUE}_.txt.gz \
        | 4_normed_matrix/add_vec_len.py \
        | awk -F'\t' '{print $2"\t"$1}' \
        | sort -t$'\t' -n -k1,1 -S500M -T ${CUR_DATA_DIR} \
        | 4_normed_matrix/create_matrix.py \
        | gzip \
        > ${CUR_DATA_DIR}/matrix_trans_${Z_VALUE}_.txt.gz

    rm ${CUR_DATA_DIR}/matrix_raw_${Z_VALUE}_.txt.gz

    zcat ${CUR_DATA_DIR}/matrix_trans_${Z_VALUE}_.txt.gz \
        | tools/create_bsddb.py ${CUR_DATA_DIR}/matrix_trans_${Z_VALUE}_.db

#    zcat ${CUR_DATA_DIR}/matrix_trans_${Z_VALUE}_.txt.gz \
#        | 4_normed_matrix/parse_vectors_length.py \
#        > ${CUR_DATA_DIR}/vectors_length.txt

#    zcat ${CUR_DATA_DIR}/matrix_trans_${Z_VALUE}_.txt.gz \
#        | 4_normed_matrix/parse_simple_vectors.py \
#        | tools/create_bsddb.py ${CUR_DATA_DIR}/matrix_trans_simple_${Z_VALUE}_.db

done

##
## NW WU-BLAST norm matrix EXCLUDE GENOME
##

export GENOME_ID="NC_010163"
export CUR_DATA_DIR=${BASE_DATA_PATH}/4_normed_matrix/${BASE_DATE}/wu_blast_nw_excl_${GENOME_ID}
mkdir -p $CUR_DATA_DIR

zcat ${BASE_DATA_PATH}/1_databases/${BASE_DATE}/genes.txt.gz   | cut -f1 | nl -v0 -n ln -w1 > ${CUR_DATA_DIR}/map_int_gene.txt
zcat ${BASE_DATA_PATH}/1_databases/${BASE_DATE}/genomes.txt.gz | cut -f1 | nl -v0 -n ln -w1 > ${CUR_DATA_DIR}/map_int_genom.txt

zcat ${BASE_DATA_PATH}/3_raw_matrix_nw/${BASE_DATE}/wu_blast/results.txt.gz \
    | awk -F'\t' '{print $2"\t"$1"\t"$3}' \
    | 4_normed_matrix/apply_maps.py \
            --genes-map ${CUR_DATA_DIR}/map_int_gene.txt \
            --genomes-map ${CUR_DATA_DIR}/map_int_genom.txt \
            --excl-genomes ${GENOME_ID} \
    | gzip \
    > ${CUR_DATA_DIR}/matrix.txt.gz

##
## WU-BLAST norm matrix
##
## Старый код, обновить перед запуском!
##

export CUR_DATA_DIR=${BASE_DATA_PATH}/3_normed_matrix_wu
mkdir -p $CUR_DATA_DIR

zcat ${BASE_DATA_PATH}/1_databases/genes.txt.gz   | cut -f1 | nl -v0 -n ln -w1 > ${CUR_DATA_DIR}/map_int_gene.txt
zcat ${BASE_DATA_PATH}/1_databases/genomes.txt.gz | cut -f1 | nl -v0 -n ln -w1 > ${CUR_DATA_DIR}/map_int_genom.txt

zcat ${BASE_DATA_PATH}/2_raw_matrix_wu_blast/results_0.05_sorted.txt.gz \
    | cut -f1,2,3 \
    | 3_normed_matrix/apply_maps.py \
        --genes-map ${CUR_DATA_DIR}/map_int_gene.txt \
        --genomes-map ${CUR_DATA_DIR}/map_int_genom.txt \
    | gzip \
    | sort -t$'\t' -n -k1,2 -S500M -T ${CUR_DATA_DIR}/ \
    | uniq \
    > ${CUR_DATA_DIR}/matrix.txt.gz

for E_VALUE in 1e-05 1e-10; do
    zcat ${CUR_DATA_DIR}/matrix.txt.gz \
    | awk -F'\t' '{if ($3<='$E_VALUE') print $1"\t"$2}' \
    | 3_normed_matrix/create_matrix.py \
    > ${CUR_DATA_DIR}/matrix_${E_VALUE}_.txt
done

